package Pokero;

import java.util.Random;



import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextPane;

public class PokerFrame extends JFrame implements ActionListener {
	private ImageIcon image;
	private JLabel label1;
        private JFrame frame;
        private JButton exit;
        private JButton next;
        public static JButton bpula;
        private JButton coins;
        public static JButton wymiana;
        private static JTextPane text;
        private static String previous;
        public static Human HumanPlayers[] = new Human[4];  // tablica przechowujaca graczy
        public static Bot[] CPUPlayers = new Bot[3];  // tablica przechowujaca graczy
        public static int PlayerNumber = 0;
        public static int CPUPlayerNumber = 0;
        public static int PlayerAmount = PlayerNumber + CPUPlayerNumber;
        public static int pula;
        public static int HumAmount = PlayerFrame.HumAmount;
        public static int money;
        public static int round = 0;
        private int Gen; //licytacja botow
        public static int PlayerBuffer = PlayerNumber;;
        
        
        public PokerFrame()  {
                
                //dane okna
                super("Pokeros");
                frame = new JFrame();
                setVisible(true);
                setSize(800,600);
                setLayout(new FlowLayout());
                setLocationRelativeTo(null);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                money = HumanPlayers[PlayerNumber].showMoney();
                
                
/*                BufferedImage myPicture = null;
                                try {
                                        myPicture = ImageIO.read(new File("/home/afro/workspace/Pokero/images/ace.png"));
                                } catch (IOException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                }
                JLabel picLabel = new JLabel(new ImageIcon(myPicture));*/
                    
                
                                
                //buttons
                coins = new JButton("Zetony: " + Integer.toString(money));
                bpula = new JButton("Pula wynosi: " + Integer.toString(pula));
                wymiana = new JButton("Wymien karty");
                wymiana.setVisible(false);
                next = new JButton("Start");
                exit = new JButton("Wyjscie");
                

                wymiana.addActionListener(this);
                next.addActionListener(this);
                exit.addActionListener(this);
               // add(label1);
                
                add(coins);
                add(bpula);

                add(wymiana);
                add(next);
                add(exit);
               // add(picLabel); 
                
                
                //panel tekstowy
                text = new JTextPane();
                                
                add(text);
                                
                Font f = new Font(Font.SANS_SERIF, 5, 17);
                text.setEditable(false);
                text.setLocation(500, 200);
                //text.setSize(500, 500);
                text.setForeground(Color.RED);
                text.setBackground(null);
                text.setBounds( 0, 0, 600, 600 );
                text.setPreferredSize( new Dimension( 700, 600 ) );  
                text.setFont(f);

                
                        
                //blokada rozmiaru okna
                this.addComponentListener(new ComponentAdapter(){
                        public void componentResized(ComponentEvent e){
                                Dimension d = PokerFrame.this.getSize();
                                if(d.width != 800)
                                        d.width = 800;
                                if(d.height != 600)
                                        d.height = 600;
                                PokerFrame.this.setSize(d);
                        }
                });
        }
        
        
        /*public void check(int money)
        {
                if(money < 5)
                {
                        five.setEnabled(false);
                        ten.setEnabled(false);
                        twenty.setEnabled(false);
                }
                else if( money < 10)
                {
                        twenty.setEnabled(false);
                        ten.setEnabled(false);
                }
                else if(money < 20)
                {
                        twenty.setEnabled(false);
                }
                
        }*/
        
        @Override
        public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                
   
                
                if(source == exit) {
                        System.exit(0);
                }
            /*    if(source == five) {
                        HumanPlayers[PlayerBuffer].money = HumanPlayers[PlayerBuffer].money - 5;
                        pula += 5;
                        money = money - 5;
                        bpula.setText("Pula wynosi: " + Integer.toString(pula));
                        coins.setText("Zetony: " + Integer.toString(money));
                        check(money);
                        //System.out.println(money);
                }
                if(source == ten) {
                        HumanPlayers[PlayerBuffer].money = HumanPlayers[PlayerBuffer].money - 10;
                        pula += 10;
                        money = money - 10;
                        bpula.setText("Pula wynosi: " + Integer.toString(pula));
                        coins.setText("Zetony: " + Integer.toString(money));
                        check(money);
                }
                if(source == twenty) {
                        HumanPlayers[PlayerBuffer].money = HumanPlayers[PlayerBuffer].money - 20;
                        pula += 20;
                        money = money - 20;
                        bpula.setText("Pula wynosi: " + Integer.toString(pula));
                        coins.setText("Zetony: " + Integer.toString(money));
                        check(money);
                }
              */  
                
                /*
                 * 
                 * okno wymiany
                 */
                if(source == wymiana) {
                        new WymianaFrame();
                }
                
                
                
                /*
                 *kolejny gracz 
                 * 
                 */
                if(source == next) {
                        
                        if(next.getText() == "Start") {
                                next.setText("Kolejny gracz");
                                wymiana.setVisible(true);
                        }
                                
                        PlayerBuffer = PlayerNumber;
                        
                        
                        //przypadek dla 1 gracza
                        if(PlayerFrame.HumAmount == 1) {
                                HumCards(0);
                            money = HumanPlayers[0].showMoney();
                            coins.setText("Zetony: " + Integer.toString(money));
                                if(round > 0 ) {
                                        
                                
                                
                                        // BOT nr 1
                                        //Random Generator = new  Random();
                                
                                        Bot.Wymiana(0);
                                        //licytacja botow
                                       // Gen = Generator.nextInt(3);
                                       // Bot.Auction(Gen, 0);
                                
                                        //BOT nr 2
                                        Bot.Wymiana(1);
                                        //licytacja botow
                                       // Gen = Generator.nextInt(3);
                                       // Bot.Auction(Gen, 1);
                                
                                        // BOT nr 3
                                        Bot.Wymiana(2);
                                        //licytacja botow
                                       // Gen = Generator.nextInt(3);
                                       // Bot.Auction(Gen, 2);
                                                
                                        new BetFrame(1);
                                }

                                new Table();
                                Table.Shuffling();   
                                Deck.pobierz();
                                HumanPlayers[0].rozdaj();
                                CPUPlayers[0].rozdaj();
                                CPUPlayers[1].rozdaj();
                                CPUPlayers[2].rozdaj();
                                
                                round++;
                        }
                                
                        // przypadek dla 2 graczy
                        if(PlayerFrame.HumAmount == 2) {
                                
                                
                                if(PlayerNumber < 2) {
                                        HumCards(PlayerNumber);
                                        money = HumanPlayers[PlayerNumber].showMoney();
                                        coins.setText("Zetony: " + Integer.toString(money));
                                        wymiana.setVisible(true);
                                        PlayerNumber++;
                                }
                                else if(PlayerNumber != 0 && PlayerNumber != 1){
                                        
                                        
                                        // BOT nr 1
                                       // Random Generator = new  Random();
                                        Bot.Wymiana(0);
                                        //licytacja botow
                                      //  Gen = Generator.nextInt(3);
                                  //      Bot.Auction(Gen, 0);
                                        
                                        //BOT nr 2
                                        Bot.Wymiana(1);
                                        //licytacja botow
                                      //  Gen = Generator.nextInt(3);
                                    //    Bot.Auction(Gen, 1);
                                        
                                        
                                        new BetFrame(2);
                                /*        
                                pula = pula + HumanPlayers[0].pobierzWpisowe();
                                pula = pula + HumanPlayers[1].pobierzWpisowe();
                                pula = pula + CPUPlayers[0].pobierzWpisowe();
                                pula = pula + CPUPlayers[1].pobierzWpisowe();
                            */
                                new Table();
                                Table.Shuffling();   
                                Deck.pobierz();
                                HumanPlayers[0].rozdaj();
                                HumanPlayers[1].rozdaj();
                                CPUPlayers[0].rozdaj();
                                CPUPlayers[1].rozdaj(); 
                                PlayerNumber = 0;
                                round++;
                                
                                }
                        }       
                        
                        //przypadek dla 3 graczy
                        if(PlayerFrame.HumAmount == 3) {
                                if(PlayerNumber < 3) {

                                        HumCards(PlayerNumber);
                                        money = HumanPlayers[PlayerNumber].showMoney();
                                        coins.setText("Zetony: " + Integer.toString(money));
                                        wymiana.setVisible(true);                               
                                        PlayerNumber++;
                                }
                                else {
                                        PlayerNumber = 0;
                                        
                                        // BOT nr 1
                                       // Random Generator = new  Random();
                                        Bot.Wymiana(0);
                                        //licytacja botow
                                  //      Gen = Generator.nextInt(3);
                                 //       Bot.Auction(Gen, 0);
                                        
                                        new BetFrame(3);
                                        /*
                                        pula = pula + HumanPlayers[0].pobierzWpisowe();
                                        pula = pula + HumanPlayers[1].pobierzWpisowe();
                                        pula = pula + HumanPlayers[2].pobierzWpisowe();
                                        pula = pula + CPUPlayers[0].pobierzWpisowe();
                                        */
                                        new Table();    
                                        Table.Shuffling();   
                                        Deck.pobierz();
                                        HumanPlayers[0].rozdaj();
                                        HumanPlayers[1].rozdaj();
                                        HumanPlayers[2].rozdaj();
                                        CPUPlayers[0].rozdaj();
                                        
                                        round++;
                                }
                        }
                        
                        
                        
                                // przypadek dla 4 graczy
                        if(PlayerFrame.HumAmount == 4) {
                                                                
                                if(PlayerNumber < 4) {
                                
                                        HumCards(PlayerNumber);
                                        money = HumanPlayers[PlayerNumber].showMoney();
                                        coins.setText("Zetony: " + Integer.toString(money));
                                        wymiana.setVisible(true);
                                        if(PlayerNumber == 4)
                                                round++;
                                        PlayerNumber++;
                                }
                                else {

                                PlayerNumber = 0;
                                
                                
                                
                                
                                
                                new BetFrame(4);
                                
                                /*pula = pula + HumanPlayers[0].pobierzWpisowe();
                                pula = pula + HumanPlayers[1].pobierzWpisowe();
                                pula = pula + HumanPlayers[2].pobierzWpisowe();
                                pula = pula + HumanPlayers[3].pobierzWpisowe();
                                */
                                new Table();    
                                Table.Shuffling();   
                                Deck.pobierz();
                                
                                HumanPlayers[0].rozdaj();
                                HumanPlayers[1].rozdaj();
                                HumanPlayers[2].rozdaj();
                                HumanPlayers[3].rozdaj();
                                round++;
                                }
                        }
                        
                        if(round == 10) {
                                if(HumAmount == 4)
                                {
                                        int winner = 0;
                                        int max = HumanPlayers[0].money;
                                        for(int which = 1; which < 5; which++)
                                        {
                                                if(max < HumanPlayers[which].money)
                                                {
                                                        max = HumanPlayers[which].money;
                                                        winner = which;
                                                }
                                        }
                                        new WinnerFrame("Zwyciezyl gracz " + HumanPlayers[winner].imie + "!!!");
                                        dispose();
                                }
                                else if(HumAmount == 3)
                                {
                                        int winner = 0;
                                        int max = HumanPlayers[0].money;
                                        for(int which = 1; which < 4; which++)
                                        {
                                                if(max < HumanPlayers[which].money)
                                                {
                                                        max = HumanPlayers[which].money;
                                                        winner = which;
                                                }
                                        }
                                        if(max < CPUPlayers[0].money)
                                        {
                                                winner = 4;
                                                new WinnerFrame("Zwyciezyl Bot " + CPUPlayers[0].imie + "!!!");
                                                dispose();
                                        }
                                        else 
                                        {
                                                new WinnerFrame("Zwyciezl gracz " + HumanPlayers[winner].imie + "!!!");
                                                dispose();
                                        }
                                }
                                else if(HumAmount == 2)
                                {
                                        int winner = 0;
                                        int gracz1 = HumanPlayers[0].money;
                                        int gracz2 = HumanPlayers[1].money;
                                        int bot1 = CPUPlayers[0].money;
                                        int bot2 = CPUPlayers[1].money;
                                        int max = gracz1;
                                        
                                        if(max < gracz2)
                                        {
                                                max = gracz2;
                                                winner = 1;
                                        }
                                        if(max < bot1)
                                        {
                                                max = bot1;
                                                winner = 2;
                                        }
                                        if(max < bot2)
                                        {
                                                max = bot2;
                                                winner = 3;
                                        }
                                        if(winner == 0 || winner == 1)
                                        {
                                                new WinnerFrame("Zwyciezyl gracz " + HumanPlayers[winner].imie + "!!!");
                                                dispose();
                                        }
                                        else 
                                        {
                                                        if(winner == 2)
                                                        {
                                                                new WinnerFrame("Zwyciezyl Bot " + CPUPlayers[0].imie + "!!!");
                                                                dispose();
                                                        }
                                                        else if(winner == 3)
                                                        {
                                                                new WinnerFrame("Zwyciezyl Bot " + CPUPlayers[1].imie + "!!!" );
                                                                dispose();
                                                        }
                                        }
                                }
                                else if(HumAmount == 1)
                                {
                                        int winner = 0;
                                        int gracz1 = HumanPlayers[0].money;
                                        int bot1 = CPUPlayers[0].money;
                                        int bot2 = CPUPlayers[1].money;
                                        int bot3 = CPUPlayers[2].money;
                                        int max = gracz1;
                                        if(max < bot1)
                                        {
                                                max = bot1;
                                                winner = 1;
                                        }
                                        if(max < bot2)
                                        {
                                                max = bot2;
                                                winner = 2;
                                        }
                                        if(max < bot3)
                                        {
                                                max = bot3;
                                                winner = 3;
                                        }
                                        if(winner == 0)
                                        {
                                                new WinnerFrame("Zwyciezyl gracz " + HumanPlayers[winner].imie + "!!!");
                                                dispose();
                                        }
                                        else
                                        {
                                                if(winner == 1)
                                                {
                                                        new WinnerFrame("Zwyciezyl Bot " + CPUPlayers[0].imie);
                                                        dispose();
                                                }
                                                else if(winner == 2)
                                                {
                                                        new WinnerFrame("Zwyciezyl Bot " + CPUPlayers[1].imie);
                                                        dispose();
                                                }
                                                else if(winner == 3)
                                                {
                                                        new WinnerFrame("Zwyciezyl Bot " + CPUPlayers[2].imie);
                                                        dispose();
                                                }
                                        }
                                }      
                        }                        
                }

       }
        
       

                //wymienia pojedyncza karte (dla botow)
        public static Card wymien() {
                Card card = Deck.wybierz();
                return card;
        }
        
        
        //zwracanie kart pojedyńczego gracza
        public static void HumCards(int PlayerNumber) {
                text.setText("Gracz numer: " + HumanPlayers[PlayerNumber].imie + "\n\n");
                        for(int i = 0; i<5; i++) {
                        previous  = text.getText();
                        text.setText(previous + HumanPlayers[PlayerNumber].Hand[i].rank + "\t" + HumanPlayers[PlayerNumber].Hand[i].suit + "\n");
                }
        }
        
        
        //zwracanie kart pojedyńczego gracza CPU
        public static void CPUCards(int PlayerNumber) {
                text.setText("Gracz numer: " + CPUPlayers[PlayerNumber].imie + " number " + PlayerNumber + "\n\n");
                        for(int i = 0; i<5; i++) {
                        previous  = text.getText();
                        text.setText(previous + CPUPlayers[PlayerNumber].Hand[i].rank + "\t" + CPUPlayers[PlayerNumber].Hand[i].suit + "\n");
                }
        }


}